<?php
if(!$aiChans){
  $aiChans=Array();
}
if(command("ai.output.text",50)){
  $text="";
  foreach($ai as $aiKey=>$aiValue){
    $text.="$aiKey\r\n";
    foreach($aiValue as $key=>$value){
      $text.="\t$key:\t$value\r\n";
    }
  }
  file_put_contents("AI.clean.txt",$text);
  SendCommand("PRIVMSG $channel :$sender: The output has been saved to AI.clean.txt");
  $text="";
}

if(command("ai.sentences")){
  $aiSize=0;
  foreach($ai as $aiA){
    $aiSize+=sizeOf($aiA);
  }
  SendCommand("PRIVMSG $channel :$sender: I know $aiSize sentences and ".sizeOf($ai)." trigger-words for those sentences as of this moment");
}
if(command("ai.save",50)){
  $paramA=explode("PRIVMSG $channel :".$messageA[0]." ai.save ",$buffer);
  $param=$paramA[1];
  if(empty($param)){
    $param=date("d-m-Y",time());
  }
  $aiLoc="data/ai_".$param.".dat";
  $aiFile=fopen($aiLoc,'w');
  fwrite($aiFile,serialize($ai));
  SendCommand("PRIVMSG $channel :Saved AI to $aiLoc");
}
if(command("ai.load",50)){
  $paramA=explode("PRIVMSG $channel :".$messageA[0]." ai.load ",$buffer);
  $param=$paramA[1];
  if(empty($param)){
    $param=date("d-m-Y",time());
  }
  $aiLoc="data/ai_".$param.".dat";
  $aiSer=file_get_contents($aiLoc);
  $ai=unserialize($aiSer);
  SendCommand("PRIVMSG $channel :Loaded AI from $aiLoc");
}

if(command("ai.clear",50)){
  $ai=Array();
  SendCommand("PRIVMSG $channel :Cleared AI data (all saved AI's are unaltered)");
}

if(command("ai.disable",50)){
  SendCommand("PRIVMSG $channel :AI disabled in $channel");
  $aiChans[strtolower($channel)]=false;
}

if(command("ai.enable",50)){
  $aiChans[strtolower($channel)]=true;
  SendCommand("PRIVMSG $channel :AI enabled in $channel");
}

if(!$ai){
	$ai=Array();
	$ai['test']=Array("meep moop","moop meep");
}
$messageA=explode("PRIVMSG $channel :",$buffer);
$message=$messageA[1];
$messageA=explode(" ",$message);
foreach($messageA as $word){
	$word = trim(str_replace(Array("!","?"),"", strtolower($word)));
	if(!stristr($word,$nickname)){
		if(!$ai[$word]){ $ai[$word]=Array(); }
		if(!in_array($message,$ai[$word])){
			$message=str_ireplace($sender,$nickname,$message);
			$ai[$word][]=$message;
			//echo "\n\n\$ai['$word'][".(sizeOf($ai[$word])-1)."]: ".$ai[$word][sizeOf($ai[$word])-1]." [adding]\n\n";
		}
		$randInt=rand(0,sizeOf($ai[$word])-1);
		$msgToSend=$ai[$word][$randInt];
		$msgToSend=trim($msgToSend);
		if(!empty($msgToSend)){
			if(!$commandSent){
				if($aiChans[strtolower($channel)]){
					if(strtolower($msgToSend)!=strtolower($message)){
						$msgToSend=str_ireplace($nickname,$sender,$msgToSend);
						$MTSA=explode(" ",$msgToSend);
						$msgEndRep=Array(":",",","?","!","");
						$mtsSS=substr($msgToSend,strlen($MTSA[0])-1,1);
						if(in_array($mtsSS,$msgEndRep)){
							$msgToSend=$sender.substr($MTSA[0],strlen($MTSA[0])-1)." ".substr($msgToSend,strlen($MTSA[0])+1);
						}
						if(!stristr($msgToSend,$sender)){
							if(substr($msgToSend,0,8)!="\001ACTION "){
								$msgToSend=$sender.", ".$msgToSend;
							}
						}
						$aiMsg="PRIVMSG $channel :".$msgToSend."\r\n";
						if(stristr($buffer,$nickname) and in_array(strtolower($channel),$aiChans)){
						//if(in_array(strtolower($channel),$aiChans)){
							fwrite($server['SOCKET'], $aiMsg, strlen($aiMsg));
							echo "[SEND] ".$aiMsg;
							sleep(2);
							$commandSent=true;
						}
					}
				}
			}
		}
	}
}
?>